import React from 'react';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import {PAGINATION_OPTIONS, TABLE, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATE_FORMAT} from '../constants';
import {injectIntl} from './intl';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import {vehicleProperty} from '../Enum';
export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }
    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };
    render() {
        const colums = [
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.code',
                    defaultMessage: '扣款单序号'
                }),
                dataIndex: 'code'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.country',
                    defaultMessage: '国家'
                }),
                dataIndex: 'country'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.secondClaimCode',
                    defaultMessage: '申请单编号'
                }),
                dataIndex: 'secondClaimCode'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.claimApplyCode',
                    defaultMessage: 'PWC号码'
                }),
                dataIndex: 'claimApplyCode'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.repairType',
                    defaultMessage: '索赔类别'
                }),
                dataIndex: 'vehicleProperty',
                render: text => conventEnumValueToString(vehicleProperty, text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.dutyUnitCode',
                    defaultMessage: '代码'
                }),
                dataIndex: 'dutyUnitCode'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.dutyUnitName',
                    defaultMessage: '索赔部门'
                }),
                dataIndex: 'dutyUnitName'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.amountTotal',
                    defaultMessage: '申请金额'
                }),
                dataIndex: 'amountTotal',
                render: text => formatAmount(text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.vin',
                    defaultMessage: 'VIN号'
                }),
                dataIndex: 'vin'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.productCategoryCode',
                    defaultMessage: '车型'
                }),
                dataIndex: 'productCategoryCode'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.engineCode',
                    defaultMessage: '发动机号'
                }),
                dataIndex: 'engineCode'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.mileage',
                    defaultMessage: '里程'
                }),
                dataIndex: 'mileage'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.saleDate',
                    defaultMessage: '购买日期'
                }),
                dataIndex: 'saleDate',
                render: text => formatDateTime(text, DATE_FORMAT)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.repairDate',
                    defaultMessage: '维修日期'
                }),
                dataIndex: 'repairDate',
                render: text => formatDateTime(text, DATE_FORMAT)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.partCode',
                    defaultMessage: '备件编号'
                }),
                dataIndex: 'partCode'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.partName',
                    defaultMessage: '备件名称'
                }),
                dataIndex: 'partName'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.laborHour',
                    defaultMessage: '工时'
                }),
                dataIndex: 'laborHour'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.quantity',
                    defaultMessage: '数量'
                }),
                dataIndex: 'quantity'
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.price',
                    defaultMessage: '单价'
                }),
                dataIndex: 'price',
                render: text => formatAmount(text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.otherAmount',
                    defaultMessage: '其他费用'
                }),
                dataIndex: 'otherAmount',
                render: text => formatAmount(text)
            }];
        const pagination = {
            total: this.props.total,
            pageSize: this.props.pageSize,
            current: this.props.pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Table
                    className="white-space-nowrap"
                    columns={colums}
                    dataSource={this.props.list}
                    onChange={this.onChangeSort}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    locale={locale}
                    {...TABLE} />
            </div>
        );
    }
}

TablePanel.propTypes = {
    errorMessage: PropTypes.string.isRequired,
    list: PropTypes.array.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};

import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';

const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    list: getData(state),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    errorMessage: state.getIn(['page', 'domainData', 'list', 'message']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
    permissions: getPermissions(state),
});

const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option))
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
